<?php

require_once($INCLUDE_PATH.'app/controller/InitController.php');
require_once ($INCLUDE_PATH . "/inc/checkfun.php");
require_once ($INCLUDE_PATH . "/include/checklogin.php");
require_once ($INCLUDE_PATH . "/include/WebService2Db.php");
function __autoload($class_name) {
    require_once APP_ROOT.'app/model/'.strtolower($class_name) . '.php';
}
class UserController extends InitController {

    public function __construct() {
        parent::__construct();

    }


    public function actionAdd() {
        $degree = CheckLogin::checkUserDegree();
        if (!$degree){
             die("<script LANGUAGE='javascript'>alert('您无权访问！');history.go(-1);</script>");
        }
        if ($_REQUEST['a']) {
            $s = array();

            $s['account'] = $_REQUEST['account'];
            $sum  = Checkfun::checkuser($s['account']) ;
            if ($sum > 0)die("<script LANGUAGE='javascript'>alert('用名户重复！');history.go(-1);</script>");
            $s['user_type'] = $_REQUEST['user_type'];

            $s['state']=$_REQUEST['state'];
            $s['email']=$_REQUEST['email'];
            $s['password']=md5($_REQUEST['password']);
            $s['company']=$_REQUEST['company'];
            $s['name']=$_REQUEST['name'];
            $s['gender']=$_REQUEST['gender'];
            $s['mobile']=$_REQUEST['mobile'];
            $s['tel']=$_REQUEST['tel'];
            $s['email']=$_REQUEST['email'];
            $s['address']=$_REQUEST['address'];
            $s['zip']=$_REQUEST['zip'];
            $s['opentime']=$_REQUEST['opentime'];
            $s['closetime']=$_REQUEST['closetime'];
            if ($_REQUEST["id"]) {

            }else {
                $s['id'] = "null";
                $result = UserModel::saveUserInfo($s);
                if ($result != "") {
                    return self::sysMessage("添加新用户成功！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
                }else {
                    return self::sysMessage("添加新用户失败！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
                }
            }
        }
     
    //echo PRE.'hhj';
    }
    public function actionList() {
        $degree = CheckLogin::checkUserDegree();
        if (!$degree){
             die("<script LANGUAGE='javascript'>alert('您无权访问！');history.go(-1);</script>");
        }
        if($_REQUEST['id']) {
            if ($_REQUEST['a'] == "del") {
                $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
                //print_r($userType);
                if($userInfo["user_type"] == 2) {
                    $del = UserModel::delUser($_REQUEST['id']);
                }else {
                    die("<script LANGUAGE='javascript'>alert('对不起，您无权删除！');history.go(-1);</script>");
                }
            }
        }
        if($_REQUEST['p']) {
            $page = $_REQUEST['p'];
        }else {
            $page = 1;
        }
        $pageSize = 2;
        $sum = UserModel::getListCount();
        if (ceil ( $sum / $pageSize ) <= $page && ceil ( $sum / $pageSize ) != 0) {
            if ($sum == 1)
            {
              $page =1;
            }else{

            $page = ceil ( $sum / $pageSize );
            }
        }
        $liststart=$pageSize * ($page - 1);
        $result = UserModel::getList($page,$liststart,$pageSize);
         $par = array();
      
        $showpage = Site_global::listPage($par, $sum, $page, $pageSize);
     
        Y::$tpl->set("list",$result);
        Y::$tpl->set("showpage",$showpage);
    }
    public function actionEdit() {
        $degree = CheckLogin::checkUserDegree();
        if (!$degree){
             die("<script LANGUAGE='javascript'>alert('您无权访问！');history.go(-1);</script>");
        }
        $id=$_REQUEST['id'];
        if ($_REQUEST['a'] == d) {
            $s = array();
   echo $_REQUEST['user_type'];
           
            $s['user_type'] = $_REQUEST['user_type'];
            $s['state']=$_REQUEST['state'];
             $s['account'] = $_REQUEST['account'];
            $s['password']=$_REQUEST['password'];
            $s['email']=$_REQUEST['email'];
            $s['company']=$_REQUEST['company'];
            $s['name']=$_REQUEST['name'];
            $s['gender']=$_REQUEST['gender'];
            $s['mobile']=$_REQUEST['mobile'];
            $s['tel']=$_REQUEST['tel'];
            $s['email']=$_REQUEST['email'];
            $s['address']=$_REQUEST['address'];
            $s['zip']=$_REQUEST['zip'];
            $s['opentime']=$_REQUEST['opentime'];
            $s['closetime']=$_REQUEST['closetime'];
            $saveUser = UserModel::editUserInfo($id,$s);
            if ($saveUser)
            {
                return self::sysMessage("修改用户成功！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }else{
               return self::sysMessage("修改用户失败！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
        }
        $userInfo = CheckFun::checkUserId($id);
        Y::$tpl->set("user",$userInfo);
    }




}

?>
